package com.tfjybj.itoo.exam.provider.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.tfjybj.itoo.exam.entity.SignInEntity;
import com.tfjybj.itoo.exam.entity.SigninDetailsEntity;
import com.tfjybj.itoo.exam.entity.StudentEntity;
import com.tfjybj.itoo.exam.model.SignInModel;
import com.tfjybj.itoo.exam.provider.dao.SignInDao;
import com.tfjybj.itoo.exam.provider.service.SignInService;
import com.tfjybj.itoo.exam.provider.service.SigninDetailsService;
import com.tfjybj.itoo.exam.provider.service.StudentService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * SignInService接口实现类
 * ${base}表
 *
 * @author 郭满亮
 * @version 2.0.0
 * @since 2.0.0 2019-03-19 09:21:41
 */
@Service("signInService")
public class SignInServiceImpl extends BaseServicePlusImpl<SignInDao, SignInEntity> implements SignInService {
	
	//region 模板生成
    @Resource
    private SignInDao signInDao;
    @Resource
    private SignInService signInService;
    @Resource
    private StudentService studentService;
    @Resource
    private SigninDetailsService signinDetailsService;
    @Resource
    private RedisTemplate redisTemplate;
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */
    /**
     * 查询历史签到记录
     * @param teacherId 教师id
     * @param teachClassId 上课班id
     * @return 签到记录
     * @author 郭满亮
     * @since 2019年3月20日09:15:45
     */
    @Override
    public ItooResult queryHistorySingin(String teacherId, String teachClassId) {
       List<SignInModel> signInModelList = signInDao.queryHistorySingin(teacherId,teachClassId);
       if (signInModelList.size() != 0) {
           return ItooResult.build(ItooResult.SUCCESS,"查询成功",signInModelList);
       }
       return ItooResult.build(ItooResult.FAIL,"暂无签到记录",signInModelList);
    }

    /**
     * 添加签到记录
     * @param signInEntity  model
     * @return 添加结果
     * @author 郭满亮
     * @since   2019年3月20日11:50:47
     */
    @Override
    public ItooResult createSignin(SignInEntity signInEntity) {
        try {
            // 生成签到表id
            String siginId = IdWorker.getIdStr();
            signInEntity.setId(siginId);
            // 添加签到记录
            signInService.save(signInEntity);
            // 获取上课班id
            String teachClassId = signInEntity.getCourseId();
            List<StudentEntity> studentEntities = studentService.getStudent(teachClassId);
            // 学生签到信息集合
            List<SigninDetailsEntity> signinDetailsEntities =  new ArrayList<>();
            for (StudentEntity studentEntity : studentEntities) {
                SigninDetailsEntity signinDetailsEntity = new SigninDetailsEntity();
                signinDetailsEntity.setSignInId(siginId);
                signinDetailsEntity.setStudentCode(studentEntity.getCode());
                signinDetailsEntity.setStudentId(studentEntity.getId());
                signinDetailsEntity.setStudentName(studentEntity.getName());
                signinDetailsEntity.setState("1");
                signinDetailsEntities.add(signinDetailsEntity);
            }
            // 添加学生签到表记录
            signinDetailsService.saveBatch(signinDetailsEntities);
            // 存放radis
            redisTemplate.opsForValue().set(teachClassId,signinDetailsEntities);
            redisTemplate.expire(teachClassId, 2400, TimeUnit.SECONDS);
            return ItooResult.build(ItooResult.SUCCESS,"添加成功");
        }
        catch (Exception e) {
            e.printStackTrace();
            return ItooResult.build(ItooResult.FAIL, "添加失败");
        }
    }

    /**
     * 签到结束,修改状态
     * @param teachClassId 上课班id
     * @return 结果
     * @author 郭满亮
     * @since 2019年3月20日14:39:22
     */
    @Override
    public ItooResult modifySigninState(String teachClassId) {
        try {
            signInDao.modifySigninState(teachClassId);
            List<SigninDetailsEntity> signinDetailsEntities = (List<SigninDetailsEntity>) redisTemplate.opsForValue().get(teachClassId);
            redisTemplate.opsForValue().set(teachClassId,signinDetailsEntities);
            redisTemplate.expire(teachClassId, 1, TimeUnit.SECONDS);
            return ItooResult.build(ItooResult.SUCCESS,"修改状态成功");
        }
        catch (Exception e) {
            e.printStackTrace();
            return ItooResult.build(ItooResult.FAIL, "修改状态失败");
        }
    }

    /**
     * 查询是否存在未结束签到
     * @param teacherId 教师id
     * @param teachClassId 上课班id
     * @return 查询结果
     * @author 郭满亮
     * @since 2019年3月24日17:57:31
     */
    @Override
    public ItooResult queryIsOffSingin(String teacherId, String teachClassId) {
        List<SignInModel> signInModelList = signInDao.queryIsOffSingin(teacherId,teachClassId);
        if (signInModelList.size() < 1) {
            return ItooResult.build(ItooResult.SUCCESS,"没有未签到记录");
        }
        return ItooResult.build(ItooResult.FAIL,"存在未签到记录");
    }
}
